<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace suifeng\builder\form;

use suifeng\helper\BaseObject;

abstract class Layout extends BaseObject
{
    /**
     * 表单对象。
     *
     * @var unknown
     */
    public $form = null;

    /**
     * 父阶对象。
     *
     * @var unknown
     */
    public $parent = null;

    /**
     * 内容数组。
     *
     * @var array
     */
    public $content = array();

    /**
     * 属性数组。
     *
     * @var array
     */
    public $attributes = array();

    /**
     * 合并配置信息。
     *
     * @param array $config
     */
    public function merge_config($config)
    {
        // 父级对象
        if (isset($config['parent'])) {
            $this->parent = $config['parent'];
            unset($config['parent']);
        }

        // 表单对象
        if (isset($config['form'])) {
            $this->form = $config['form'];
            unset($config['form']);
        }

        // 父级对象和表单对象相同的情况
        if ($this->form !== null && $this->parent === null) {
            $this->parent = $this->form;
        } else if ($this->form === null && $this->parent !== null) {
            if ($this->parent instanceof \suifeng\builder\FormBuilder) {
                $this->form = $this->parent;
            } else if (isset($this->parent->form) && $this->parent->form) {
                $this->form = $this->parent->form;
            }
        }

        parent::merge_config($config);
    }

//     /**
//      * 初始化内容数组。
//      *
//      * @param unknown $config
//      */
//     protected function _init_content($config)
//     {
//         $this->content = array();

//         foreach ($config as $item) {
//             if (is_array($item)) {
//                 $child = self::factory($item);
//                 $child->form = $this->form;
//                 $child->parent = $this;
//                 $this->content[] = $child;
//             } else {
//                 $this->content[] = $item;

//                 if ($item instanceof Layout) {
//                     $item->parent = $this;
//                     $item->form = $this->form;
//                 }
//             }
//         }
//     }

    /**
     * 输出HTML。
     *
     * @param string $template
     * @param string $variables
     * @return string
     */
    public abstract function render_html();

    /**
     * 转换成字符串。
     *
     * @return string
     */
    public function __toString()
    {
        return $this->render_html();
    }

    /**
     * 工厂方法。
     *
     * @param unknown $form
     * @param string $config
     * @throws \Exception
     * @return mixed
     */
    public static function factory($type, $config = array())
    {
        if (is_array($type)) {
            $attributes = $type;

            if (isset($attributes['layout'])) {
                $type = $attributes['layout'];
                unset($attributes['layout']);
            } else {
                $type = 'vertical';
            }
        }

        $class_name = '\\suifeng\\builder\\form\\layout\\' . implode('', array_map('ucfirst', explode('_', $type)));
        if (!class_exists($class_name)) {
            throw new \Exception('Layout type not exists: ' . $type);
        }

        $obj = new $class_name($config);
        return $obj;
    }
}
